package cn.hesion.outputformat;
/**
 * ClassName: CustomWriter <br/>
 * Description: <br/>
 * date: 2021/2/3 19:17<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

/**
 * @program: ClientDemo
 * @description:
 * @author: hesion
 * @create: 2021-02-03 19:17
 **/
public class CustomWriter extends RecordWriter<Text, NullWritable> {
    private FSDataOutputStream lagouOut;
    private FSDataOutputStream otherOut;

    public CustomWriter(FSDataOutputStream lagouOut, FSDataOutputStream otherOut) {
        this.lagouOut = lagouOut;
        this.otherOut = otherOut;
    }

    @Override
    public void write(Text key, NullWritable nullWritable) throws IOException, InterruptedException {
        if (key.toString().contains("lagou")) {
            lagouOut.write(key.toString().getBytes());
            lagouOut.write("\r\n".getBytes());
        }else {
            otherOut.write(key.toString().getBytes());
            otherOut.write("\r\n".getBytes());
        }
    }

    @Override
    public void close(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
        IOUtils.closeStream(lagouOut);
        IOUtils.closeStream(otherOut);
    }
}
